package com.dt.common.validation.definition;

import com.dt.common.validation.annotation.NotBlank;
import com.dt.common.validation.impl.DefaultValidationDefinition;
import com.dt.common.validation.itf.ConstraintValidation;
import com.dt.common.validation.itf.ValidationContext;

public class NotBlankDefinition extends DefaultValidationDefinition {

	public NotBlankDefinition() {
		this.name = NotBlank.class.getSimpleName();
	}

	@Override
	public ConstraintValidation buildValidation() {

		return new ConstraintValidation() {

			@Override
			public boolean isValid(ValidationContext context, Object objectValue, Object propertyValue) {
				if (propertyValue == null) {
					return false;
				}

				if (CharSequence.class.isAssignableFrom(propertyValue.getClass())) {
					CharSequence value = (CharSequence) propertyValue;

					return !(value.toString().trim().isEmpty());
				}

				return false;
			}
		};
	}
}
